<template>
  <div class='ele-body ele-body-card'>
    <a-card
      title="参数设置"
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='gatewaytimeformRef'
        :model='gatewaytimeformModel'
        :rules="gatewaytimeRules"
        :label-col='{ md: { span: 16 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 8 }, sm: { span: 24 } }'
        @submit='pushHeartInterval'
      >
        <a-alert message="注意：如果第一路(温度)上下限没开启，喷淋开启是不管用" type="warning" />
        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label="温度上限报警开启:" name="highAlarmSwitch">
              <a-select v-model:value="gatewaytimeformModel.highAlarmSwitch">
                <a-select-option :value="0">两路同时关闭</a-select-option>
                <a-select-option :value="1">第一路(温度)上限开启</a-select-option>
                <a-select-option :value="2">第二路(湿度)上限开启</a-select-option>
                <a-select-option :value="3">两路同时开启</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label="温度下限报警开启:" name="lowAlarmSwitch">
              <a-select v-model:value="gatewaytimeformModel.lowAlarmSwitch">
                <a-select-option :value="0">两路同时关闭</a-select-option>
                <a-select-option :value="1">第一路(温度)上限开启</a-select-option>
                <a-select-option :value="2">第二路(湿度)上限开启</a-select-option>
                <a-select-option :value="3">两路同时开启</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='报警温度上限(整数):' name='tempAlarmHigh'>
              <a-input v-model:value='gatewaytimeformModel.tempAlarmHigh' placeholder='报警温度上限' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='报警温度下限(整数):' name='tempAlarmLow'>
              <a-input v-model:value='gatewaytimeformModel.tempAlarmLow' placeholder='报警温度下限' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='合格温度(报警湿度)上限(整数):' name='standardTempAlarmHigh'>
              <a-input v-model:value='gatewaytimeformModel.standardTempAlarmHigh' placeholder='请输入合格温度(报警湿度)上限，单位℃' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='合格温度(报警湿度)下限(整数):' name='standardTempAlarmLow'>
              <a-input v-model:value='gatewaytimeformModel.standardTempAlarmLow' placeholder='请输入合格温度(报警湿度)下限，单位℃' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='采集间隔时间(分钟,范围1-2880):' name='collectTime'>
              <a-input v-model:value='gatewaytimeformModel.collectTime' placeholder='采集间隔时间，单位分钟' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='发送间隔时间(分钟,范围1-2880):' name='sendTime'>
              <a-input v-model:value='gatewaytimeformModel.sendTime' placeholder='发送间隔时间，分钟' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label="喷淋使能:" name="sprayWork">
              <a-select v-model:value="gatewaytimeformModel.sprayWork">
                <a-select-option :value="0">无效</a-select-option>
                <a-select-option :value="1">有效</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24' />
          <a-col :md='2' :sm='24' :xs='24'>
            <a-button v-permission="['PROPERTY_DEVICELIST_MGR_DRINKSTATION_CONFIG_SAVE_BUTTON']" type='primary' html-type='submit' :loading='gatewaytimeformRefLoading'>
              提交
            </a-button>
          </a-col>
          <a-col :md='1' :sm='24' :xs='24'/>
          <a-col :md='5' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>
  </div>
</template>

<script>
import {DeviceDrinkingStationConfigApi} from "@/api/property/drinkingstation/DeviceDrinkingStationConfigApi";
import {DeviceCommandDownQueueApi} from '@/api/property/DeviceCommandDownQueueApi';
import { message } from 'ant-design-vue';

export default {
  name: 'DeviceDrinkstationConfigOther',
  components: {
  },
  props: {
    data: Object
  },
  async created() {
    await this.init();
  },
  data() {
    return {
      loading : false,
      stationConfig : {},

      gatewaytimeformModel : {},
      gatewaytimeRules : {},
      gatewaytimeformRefLoading : false,
      gatewaytimeformRefTimer : undefined,

    };
  },
  methods: {
    getDetail() {
      let that = this;
      DeviceDrinkingStationConfigApi.detail({"recordId" : this.data.recordId}).then(result => {
        that.stationConfig = result || {};
        this.gatewaytimeformModel = that.stationConfig;
      });
    },
    async init() {
      this.getDetail();
    },

    clearGatewaytimeformRef(){
      this.gatewaytimeformRefLoading = false;
      clearInterval(this.gatewaytimeformRefTimer);
    },

    checkProcess(dictate, clearCallback, callback) {
      let params = {
        recordId: this.data.recordId,
        sceneDictate: dictate
      };
      let result = DeviceCommandDownQueueApi.workingDetail2(params);
      result
        .then(result => {
          if (!result || !result.dcdqId) { //处理完成了
            clearCallback && clearCallback(); //去掉菊花+清除循环调用
            callback && callback(); //用户自定义
          }
        })
        .catch(() => {
          clearCallback && clearCallback(); //去掉菊花+清除循环调用
        });
    },

    checkHeartInterval(){
      let that = this;
      this.checkProcess(1, this.clearGatewaytimeformRef, function(){
        that.init();
      });
    },
    pushHeartInterval() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 1
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail2(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          console.log(workResult);
          if (!workResult || !workResult.dcdqId) { //处理完成了
            that.pushHeartIntervalSubmit();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },

    pushHeartIntervalSubmit() {
      this.loading = true;
      if (this.gatewaytimeformModel.tempAlarmLow < -50 || this.gatewaytimeformModel.tempAlarmLow > 100) {
        message.error('温度上限应该在-50-100℃');
        return;
      }
      if (this.gatewaytimeformModel.tempAlarmHigh < -50 || this.gatewaytimeformModel.tempAlarmHigh > 100) {
        message.error('温度下限应该在-50-100℃');
        return;
      }

      // if (this.gatewaytimeformModel.tempAlarmHigh > this.gatewaytimeformModel.tempAlarmLow) {
      //   message.error('上限应该大于下限');
      //   return;
      // }
      if (this.gatewaytimeformModel.collectTime < 1 || this.gatewaytimeformModel.collectTime > 2880) {
        message.error('采集时间范围1-2880分钟');
        return;
      }
      if (this.gatewaytimeformModel.sendTime < 1 || this.gatewaytimeformModel.sendTime > 2880) {
        message.error('发送时间范围1-2880分钟');
        return;
      }

      let updateDataRecord = {
        recordId: this.data.recordId,
        templateCode: 'drinkstation',
        updateData: JSON.stringify({ old: this.stationConfig, update: this.gatewaytimeformModel}),
        dictate : 1,
        sceneDictate : 1
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.gatewaytimeformRefLoading = true; //转圈
          this.gatewaytimeformRefTimer = setInterval(this.checkHeartInterval, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style scoped>
/** 用户卡片 */
.workplace-user-card .ele-cell-content {
  overflow: hidden;
}

.workplace-user-card h4 {
  margin-bottom: 6px;
}
</style>
